package com.agent.web;

import com.agent.entity.User;
import com.agent.page.EasyuiPage;
import com.agent.service.UserService;
import com.agent.shiro.ShiroUser;
import com.agent.util.Constants;
import com.agent.util.Md5Utils;
import com.agent.util.ResponseData;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

import static com.agent.util.ResponseData.getSuccessResult;

/**
 * Created by hg on 2019/1/16.
 */
@Controller
@RequestMapping(value = "/user")
public class UserController {

    @Resource(name = "userServiceImpl")
    private UserService userService;


    @RequestMapping(method = RequestMethod.GET)
    public String toHtml(HttpServletRequest request){
        request.setAttribute("addressMap",Constants.addressMap);
        request.setAttribute("rankMap",Constants.rankMap);
        return "system/user";
    }


    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ResponseBody
    public EasyuiPage getKnowledgeList(Integer page, Integer rows, User user) {
        EasyuiPage<User> lists = userService.searchUsers(page,rows,user);
        return lists;
    }


    @RequestMapping(value="/delete/{id}")
    @ResponseBody
    public Map<String,Object> delete(@PathVariable("id") String id) {
        userService.delete(id);
        return getSuccessResult();

    }

    @RequestMapping(value="/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> update(@PathVariable("id") Integer id, User user,HttpServletRequest request) {
        user.setId(id);
        userService.update(user);
        return getSuccessResult();
    }

    @RequestMapping(value="/insert",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> insert(User user, BindingResult result, HttpServletRequest request) {
        user.setCreateTime(new Date());
        ShiroUser shiroUser = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
        user.setCreator(shiroUser.getUserName());
        user.setEnabled(true);
        user.setPassword(Md5Utils.encryptPassword(Constants.INIT_PASSWORD));
        List<User> list = new ArrayList<User>();
        list.add(user);
        userService.insert(list);
        return getSuccessResult();
    }

    @RequestMapping("/find/{telphone}")
    public String findOne(@PathVariable("telphone") String telphone, Model model){
        Map<String,String> paramMap = new HashMap<>();
        if(telphone.length()==11){
            paramMap.put("telphone",telphone);
        }else{
            paramMap.put("extension",telphone);
        }
        User user = userService.findOne(paramMap);
        model.addAttribute("user",user);
        return "soft-phone/callerInfo";
    }

    @RequestMapping("/user")
    @ResponseBody
    public Map<String,Object> findUser( User user, Model model){
        List<User> users = userService.findUser(user);
        Map<String, Object> successResult = getSuccessResult();
        successResult.put(ResponseData.DATA,users);
        return successResult;
    }

    @RequestMapping("/findByEmail")
    @ResponseBody
    public Map<String,Object> findUser(@RequestParam("keywords") String email , Model model){
        List<User> users = userService.findByEmail(email);
        Map<String, Object> successResult = getSuccessResult();
        successResult.put("type","success");
        successResult.put("code",0);
        successResult.put("content",users);
        return successResult;
    }


}
